<template>
	<view class="bgIndex ">

		<view class="uni-textarea">
			<textarea v-model="form.title" placeholder-style="color:#999;" placeholder="真诚的表达，是得到互动的必杀技" />
		</view>
		<sunui-upimg1 class="margin-top_20rpx" :border="'1px dashed #bfbfbf'" :formData="imgFormData"
			:storageMode="storageMode" :header="header" :size="['200rpx', '200rpx']" btnName="添加照片"
			:uploadServerUrl="upPicUrl" ref="selectimgandupload1" @uploaded="imgLoaded1" @change="imgsChange1"
			:maxFileNumber="5"><template v-slot:icon>
				<uni-icons type="camera-filled" color="#1890FF" size="40"></uni-icons>
			</template></sunui-upimg1>
		<view class="flex-between" style="margin-top: 60rpx;">
			<view class="flex-start-center ">
				<view class="addreeBtn flex " style="margin-right: 5rpx;" @click="location()">
					<image class="address-img" src="/static/组合 1625.png" mode="widthFix"></image>
					<!-- <image class="address-img" :src="" mode=""></image> -->
					<view style="max-width: 288rpx;" class="font-overflow">
						<text>{{address?address:'获取位置'}}</text>
					</view>
				</view>
				<view class="addreeBtn flex" @click="followMe()">
					<text style="max-width: 200rpx;" class="font-overflow">{{He}}</text>
				</view>
			</view>

			<view class="addreeBtn flex" @click="whoCanSeeIt()">
				<text>{{whoLook}} </text>
			</view>

		</view>
		<view class="flex">
			<view class="releaseBtn flex" @click="releaseFn()">
				立即发布
			</view>
		</view>

		<sunui-popup ref="modifyEmployeeInfo">
			<template v-slot:content>

				<view class="moreActiveButton">
					<view class="add" @click.stop="authorityFn(1)">
						所有人
					</view>
					<view class="add" @click.stop="authorityFn(2)">
						关注的人
					</view>
					<view class="add" @click.stop="authorityFn(3)">
						自己
					</view>

				</view>

			</template>
		</sunui-popup>

	</view>
</template>

<script>
	export default {
		components: {

		},
		data() {
			return {
				dw: this.$img.dw,
				userInfo: uni.getStorageSync("userInfo"),
				title: 'Hello',
				seconds: '',
				showBtn: false,
				form: {
					title: '',
					img: '',
					longitude: '',
					dimension: '',
					authority: "1",
					notice_user: '',
					address: ''
				},
				imgFormData: null,
				storageMode: "2",
				header: null,
				upPicUrl: null,
				img: [],
				imgStr: [],
				address: '获取位置',
				whoLook: '谁可以看',
				He: '@Ta',
				isEditApi: false,
				basicConfiguration: {},
				userInfoObj: null

			}
		},
		onLoad(options) {
			this.userInfoFn()
			if (options.id) {

				this.circleInfoFn(options.id)
				this.isEditApi = true
			}
			this.basicConfigurationFn()
		},
		onShow() {
			if (uni.getStorageSync("idString").length) {
				this.He = uni.getStorageSync("idString").map((item) => {
					return item.nickname

				}).join(',')

				this.form.notice_user = uni.getStorageSync("idString").map((item) => {
					return item.id

				}).join(',')
			} else {
				this.form.notice_user = '',
					this.He = '@Ta'
			}
		},
		methods: {
			async basicConfigurationFn() {
				const result = await this.$http.post({
					url: this.$api.config,
					data: {


					},
					headers: {
						'Authorization': this.userInfo.token
					}

				})
				if (result.code == 0) {
					console.log(result)
					wx.setStorageSync('system', result.data);
					this.basicConfiguration = result.data
					
					if (uni.getStorageSync("userInfo").level_id == 1 ? (!Number(this.basicConfiguration.circle_number)) : (
							!Number(this.basicConfiguration.level_circle_number))) {
						this.$sun.toast("发布圈子已达上限", 'none')
					} else if (uni.getStorageSync("userInfo").level_id == 1 ? (Number(uni.getStorageSync("userInfo").circleNumber) >= Number(
							this.basicConfiguration.circle_number)) : (Number(uni.getStorageSync("userInfo").circleNumber) >=
							Number(this.basicConfiguration
								.level_circle_number))) {
						this.$sun.toast("发布圈子已达上限", 'none')
					
					}

				}
			},
			async circleInfoFn(id) {
				const res = await this.$http.post({
					url: this.$api.circleInfo,
					data: {
						id: id
					},
					headers: {
						'Authorization': this.userInfo.token
					}

				})
				if (res.code == 0) {
					console.log(res.data)

					this.$sun.assignObject(this.form, {
						...res.data
					})
					this.$nextTick(() => {
						this.form.id = id
						this.form.status = 1
						if (res.data.authority == 1) {
							this.whoLook = '所有人'
						} else if (res.data.authority == 1) {
							this.whoLook = '关注的人'
						} else if (res.data.authority == 2) {
							this.whoLook = '自己'
						}
						this.address = this.form.address
						const cover_imgList = res.data.img.split(',').map((item) => item.trim());
						console.log(cover_imgList)
						this.$refs.selectimgandupload1.setItems(cover_imgList);
						res.data.notice_user_list.forEach((item, index) => {
							res.data.notice_user_list[index].checked = true
						})
						uni.setStorageSync('idString', res.data.notice_user_list)
						this.He = res.data.notice_user_list.map((item) => {
							return item.nickname

						}).join(',')
						console.log(this.He)

					})

				} else {
					this.$sun.toast(res.message, 'none')
				}
			},
			async userInfoFn() {
				const result = await this.$http.post({
					url: this.$api.userInfo,
					data: {

						// id:this.userInfo.id


					},
					headers: {
						'Authorization': this.userInfo.token
					}

				})

				if (result.code == 0) {
					console.log('data', result.data);
					wx.setStorageSync('userInfo', result.data);
					this.userInfoObj = result.data



				}
			},
			async releaseFn() {
				if (uni.getStorageSync("userInfo").disable == 0) {
					this.$sun.toast('该用户已被拉黑', 'none')
				} else {
					if (uni.getStorageSync("userInfo").real_name || (!Number(uni.getStorageSync("system")
							.isOpen_idCard_authentication))) {
						console.log(this.form)
						if (this.userInfoObj.level_id == 1 ? (!Number(this.basicConfiguration.circle_number)) : (
								!Number(this.basicConfiguration.level_circle_number))) {
							this.$sun.toast("发布圈子已达上限", 'none')
						} else if (this.userInfoObj.level_id == 1 ? (Number(this.userInfoObj.circleNumber) >= Number(
								this.basicConfiguration.circle_number)) : (Number(this.userInfoObj.circleNumber) >=
								Number(this.basicConfiguration
									.level_circle_number))) {
							this.$sun.toast("发布圈子已达上限", 'none')

						} else if (!(this.form.img || this.form.title)) {
							this.$sun.toast('请上传图片或者输入内容', 'none')
						} else if (!(this.form.longitude && this.form.dimension)) {
							this.$sun.toast('请选择你的位置', 'none')
						} else {
							const resApi = this.isEditApi ? this.$api.updateCircle : this.$api.addCircle
							const res = await this.$http.post({
								url: resApi,
								data: {
									...this.form
								},
								headers: {
									'Authorization': this.userInfo.token
								}

							})
							if (res.code == 0) {
								console.log(res)

								this.$sun.toast(res.message, 'none')
								uni.removeStorageSync('idString')
								setTimeout(() => {
									this.$sun.navBack()
								}, 600)
							} else {
								this.$sun.toast(res.message, 'none')
							}
						}
					} else {
						this.$sun.toast('请实名认证后在发布圈子', 'none')

						setTimeout(() => {
							this.$sun.navGo('/pagesA/personalCenter/informationIdCard')

						}, 1000)
					}
				}





			},
			authorityFn(index) {
				this.form.authority = index
				if (index == 1) {
					this.whoLook = '所有人'
				} else if (index == 2) {
					this.whoLook = '关注的人'
				} else if (index == 3) {
					this.whoLook = '自己'
				}
				this.$refs.modifyEmployeeInfo.close()
			},
			location() {
				this.$sun.chooseLocation({
					lat: this.userInfo.dimension,
					lng: this.userInfo.longitude
				}).then((res) => {
					console.log(res)
					this.address = res.address;
					this.form.address = res.address;
					this.form.dimension = res.latitude;
					this.form.longitude = res.longitude;

				})
				// uni.chooseLocation({
				// 	success: res => {
				// 		this.address = res.name;
				// 		this.form.address = res.name;
				// 		this.form.dimension = res.latitude;
				// 		this.form.longitude = res.longitude;
				// 		console.log('获取定位信息res============', res);
				// 	}
				// });
			},
			followMe() {

				this.$sun.navGo('/pages/index/followList')

			},
			// 上传图片返回
			imgLoaded1(e) {
				console.log(e)

				this.img = [];
				for (let i = 0; i < e.length; i++) {
					this.img.push(e[i].url);
				}


				this.imgStr = this.img

				this.form.img = this.imgStr.length === 1 ? this.imgStr[0].toString() : this.imgStr
					.join(',');
			},
			imgsChange1(file) {

				console.log('上传发生改变this.storageMode1111============', this.storageMode, '参数file', file);


				if (file.length) {
					this.upPicUrl = this.$api.upload;
					let filesNmae = 'nmoBackground_01.png'; // this.timeDate + 'png'
					this.imgFormData = {
						file: file,
						getsize: file.size,
						getContentType: ['image/png', 'image/jpeg'],
						getoriginalfilename: filesNmae,
						getName: filesNmae
					};
					this.$nextTick(() => {
						this.$refs.selectimgandupload1.upload(); //图片上传
					});
				} else {
					this.form.img = '',
						this.imgStr = []
				}
				// this.niuyunLoop(file,0)

			},
			// 谁可以看
			whoCanSeeIt() {
				this.$refs.modifyEmployeeInfo.show({
					anim: 'bottom',
					position: 'bottom',
					shadeClose: true, //false使用户不能点击其它关闭页面
					topclose: false
				});

			}
		}
	}
</script>

<style lang="less" scoped>
	.flex-center() {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.moreActiveButton {


		background-color: #fff;


		.add {
			height: 100rpx;
			color: #6568F6;
			font-size: 28rpx;
			font-family: Inter;

			.flex-center();
			border: 1px solid #f7f7f7;


		}

		.delete {
			.flex-center();
			height: 100rpx;


			color: #FF4636;
			font-size: 28rpx;
			font-family: Inter;
			box-shadow: 2rpx 2rpx 2rpx 30rpx 30rpx rgba(238, 238, 238, 1.0);
		}


	}

	.address-img {
		margin-top: 5rpx;
		width: 25rpx;
		height: 25rpx;
		margin-right: 10rpx;

	}

	.bgIndex {
		padding: 20rpx;

		.uni-textarea {
			margin-top: 36rpx;
			margin-bottom: 60rpx;

			textarea {
				height: 800rpx;
				min-height: 500rpx;
				width: 100%;
				box-sizing: border-box;

				border-radius: 10px;
				background-color: #F0F0F0;
				padding: 15rpx;
			}
		}

		.addreeBtn {

			height: 56rpx;
			padding: 8rpx 16rpx;
			box-sizing: border-box;
			border: 2rpx solid rgb(227, 226, 226);
			border-radius: 104px;
			color: rgb(153, 153, 153);
			background: rgb(255, 255, 255);
		}

		.releaseBtn {
			margin-top: 80rpx;
			color: #fff;
			width: 734rpx;
			height: 110rpx;

			border-radius: 164rpx;

			background: linear-gradient(96.84deg, rgb(62, 140, 252) 0.132%, rgb(125, 93, 255) 99.899%);
		}
	}
</style>